/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import javax.swing.JOptionPane;


/**
 * Clase JFrame encargada de mostrar las opciones para borrar una categoría.
 * @author Victoria Fuenmayor y Jose Gabriel Rivera
 */
public class JFrameBorrarCat extends javax.swing.JFrame {

    private JPanelCategoria panelCat;
    private GUI gui;
    
/**
 * Contructor. Inicializa los componentes del JFrame, asigna el GUI al atributo gui,
 * crea un nuevo panel selector de categorias sobreescribiendo el metodo que se ejecuta
 * en el listenner para adptarlo a este JFrame. Agrega el selector al frame y lo muestra.
 * 
 * @param gui Recibe el gui principal.
 */
    public JFrameBorrarCat(final GUI gui) {
        initComponents();
       
        this.gui = gui;
        panelCat = new JPanelCategoria(gui.getCategoria(),this.gui){

            @Override
            public void evento() {
                if(panelCat.getIdCat() == -1){
                    return;
                }                
                if(((logic.Categoria)(gui.getCategoria().getObject(panelCat.getIdCat()))).getRifas().getLength() >0) {
                   JOptionPane.showMessageDialog(gui.getListaCat(), "Advertencia: Esta categoría tiene rifas creadas" + '\n'
                           + "Si la elimina se perderán las rifas que contiene");
                }
            }
            
        };
        this.add(panelCat);
        panelCat.setBounds(10,20,200,22);
        this.setLocation(280, 210);
        this.setVisible(true);
    }

    /**
     * Getter para el panel selector de categorias
     * @return Retorna el panel selector de categorias ( JPanelCategoria )
     */
    public JPanelCategoria getPanelCat() {
        return panelCat;
    }
    
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        jButton1.setText("Borrar");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(23, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(189, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * MouseClicked para el boton cancelar. Si se presiona oculta la ventana.
     * @param evt 
     */
    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_jButton2MouseClicked

    /**
     * MouseClicked para el boton de borrar. Toma la categoria que esté seleccionada,
     * verifica que no sea la categoria nula, y llama al metodo que borra la categoria
     * con el id que indique el selector de categorias. Luego actualiza la lista de categorias
     * en el gui y oculta este JFrame.
     * 
     * Si la categoria seleccionada es nula muestra un mensaje de error.
     * 
     * @param evt 
     */
    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        // TODO add your handling code here:
        if(this.panelCat.getIdCat()!=-1){
            gui.getCategoria().removeObject(panelCat.getIdCat());
            gui.getListaCat().CargarCat();
            this.setVisible(false);
        }else{
            JOptionPane.showMessageDialog(panelCat, "Error: Selecione una categoría");
        }
    }//GEN-LAST:event_jButton1MouseClicked

    
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JFrameBorrarCat.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JFrameBorrarCat.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JFrameBorrarCat.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JFrameBorrarCat.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JFrameBorrarCat(new GUI ("Raffle System")).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    // End of variables declaration//GEN-END:variables
}
